﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using WebApplication1.ClientServiceReference;

namespace WebClient
{
    public partial class WebClient : System.Web.UI.Page
    {
        
        private static IBetterClientService clientService = new BetterClientServiceClient();
        private List<Station> listStations = new List<Station>();


        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void DropDownStartStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownEndStation.Items.Clear();
            foreach (var item in DropDownStartStation.Items)
            {
                if (DropDownStartStation.SelectedItem != item)
                {
                    DropDownEndStation.Items.Add((ListItem) item);
                }
            }
            
            DropDownEndStation.Enabled = true;
        }
        private Customer GetCustomerByName(string name)
        {
            return null;
        }

        private Station GetStationByName(string name)
        {
            return (from s in listStations
                    where s.Name == name
                    select s).FirstOrDefault();
        }

        protected void btnFindRoute_Click(object sender, EventArgs e)
        {
            if (DropDownEndStation.SelectedIndex > -1 && DropDownStartStation.SelectedIndex > -1 &&
                DropDownCustomer.SelectedIndex > -1)
            {
                btnFindRoute.Enabled = false;

                var startId = Int32.Parse(DropDownStartStation.SelectedItem.Value);
                var endId = Int32.Parse(DropDownEndStation.SelectedItem.Value);
                var custId = Int32.Parse(DropDownCustomer.SelectedItem.Value);

                if (startId != endId)
                {
                    clientService.CreateReservationUsingDijkstras(startId, endId, custId, DateTime.Now);
                }
            }
            
        }

        protected void btnreslink_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebReservation.aspx");
        }
    }
}
namespace WebApplication1.ClientServiceReference
{
    public partial class Station
    {
        public override string ToString()
        {
            return Name;
        }

        public IEnumerable<Station> GetAllStations()
        {
            var clientService = new BetterClientServiceClient();
            List<Station> stations = null;
            var stationList = clientService.GetAllStation();
            if (stationList.SerStatus.Equals("Ok"))
            {
                stations = new List<Station>(stationList.SerData);
            }
            return stations;
        }
    }

    public partial class Reservation
    {
        public IEnumerable<Reservation> GetAllReservations()
        {
            var clientService = new BetterClientServiceClient();
            var resvList = clientService.GetAllReservationsFromCustomer(2);
            return resvList.SerStatus.Equals("Ok") ? new List<Reservation>(resvList.SerData) : null;
        }
    }

    public partial class Route
    {
        public override string ToString()
        {
            return StartStation + " til " + EndStation;
        }
    }

    public partial class Customer
    {
        public override string ToString()
        {
            return Name;
        }

        public IEnumerable<Customer> GetAllCustomers()
        {
            var clientService = new BetterClientServiceClient();
            List<Customer> customers = null;
            var personList = clientService.GetAllCustomer();
            if (personList.SerStatus.Equals("Ok"))
            {
                customers = new List<Customer>(personList.SerData);
            }
            return customers;
        }
    }

    //public partial class Node
    //{
    //    public override string ToString()
    //    {
    //        return Station.ToString();
    //    }
    //}
}